home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / GETARGS.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  3KB  |  88 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    g e t a r g s . c                                               */
  3. /*                                                                    */
  4. /*    Support routines for UUPC/extended                              */
  5. /*                                                                    */
  6. /*    Changes Copyright 1990, 1991 (c) Andrew H. Derbyshire           */
  7. /*                                                                    */
  8. /*    History:                                                        */
  9. /*       21Nov1991 Break out of lib.c                          ahd    */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. #include <ctype.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <time.h>
  17.  
  18. /*--------------------------------------------------------------------*/
  19. /*                    UUPC/extended include files                     */
  20. /*--------------------------------------------------------------------*/
  21.  
  22. #include "lib.h"
  23.  
  24. /*--------------------------------------------------------------------*/
  25. /*    g e t a r g s                                                   */
  26. /*                                                                    */
  27. /*    Return a list of pointers to tokens in the given line           */
  28. /*--------------------------------------------------------------------*/
  29.  
  30. int getargs(char *line, char **flds)
  31. {
  32.    int i = 0;
  33.    char quoted = '\0';
  34.  
  35.    while ((*line != '\0') && (*line != '\n')) {
  36.       if (isspace(*line))
  37.          line++;
  38.       else {
  39.          char *out = line;
  40.          *flds++ = line;
  41.          i++;
  42.          while((quoted || !isspace(*line)) && (*line != '\0'))
  43.          {
  44.             switch(*line)
  45.             {
  46.                case '"':
  47.                case '\'':
  48.                   if (quoted)
  49.                   {
  50.                      if (quoted == *line)
  51.                      {
  52.                         quoted = 0;
  53.                         line++;
  54.                      }
  55.                      else
  56.                         *out++ = *line++;
  57.                   } /* if */
  58.                   else
  59.                      quoted = *line++;
  60.                   break;
  61.  
  62.                case '\\':
  63.                   switch(*++line)         /* Unless the following    */
  64.                   {                       /* character is very       */
  65.                      default:             /* special we pass the \   */
  66.                         if (!isspace(*line))
  67.                            *out++ = '\\'; /* and following char on   */
  68.                      case '"':
  69.                      case '\'':
  70.                         *out++ = *line++;
  71.                   }
  72.                   break;
  73.  
  74.                default:
  75.                   *out++ = *line++;
  76.  
  77.             } /*switch*/
  78.          } /* while */
  79.          if (isspace(*line))
  80.             line++;
  81.          *out = '\0';
  82.       } /* else */
  83.    }
  84.  
  85.    return i;
  86.  
  87. } /*getargs*/
  88.